/* 
Scenic Graphics Library
Copyright (C) 2007 Jouni Tulkki

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USApackage scenic;
*/

package test;

import java.awt.Frame;
import java.awt.Rectangle;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import scenic.ImageInfo;
import scenic.SceneContainer;
import scenic.ScenicCanvas;
import scenic.Color;
import scenic.Graphics;
import scenic.Image;
import scenic.show.SimpleMediaFile;

@SuppressWarnings("serial")
public class Capture  extends ScenicCanvas {
	SimpleMediaFile player;
	
	public Capture() {
		setBackground(new Color(0.5f));
	}
	
	public void updateScene() {
		SceneContainer scene = new SceneContainer();		
		Graphics g = new Graphics(scene);
		setScene(scene);
		
		if(player == null) {
			player = new SimpleMediaFile("capture://");
			ImageInfo outputFormats[] = player.getOutputFormats();
			ImageInfo format = outputFormats[outputFormats.length - 1];
			
			for(int i = 0; i < outputFormats.length; i++) {
				if(outputFormats[i].width >= 640 && 
						outputFormats[i].height >= 480) {
					format = outputFormats[i];
					break;
				}
			}
			player.setOutputFormat(format.width, format.height, format.format);
			player.play();
		}
		
		Image image = player.getImage();
		double scale = Math.min((double)getWidth() / image.getWidth(),
				(double)getHeight() / image.getHeight());
		g.translate((getWidth() - image.getWidth() * scale) / 2, 
				(getHeight() - image.getHeight() * scale) / 2);
		g.scale(scale, scale);
		g.drawImage(image);
	}
	
	public static void createWindow() {
		final Frame frame = new Frame();
		ScenicCanvas canvas = new Capture();
		
		frame.setTitle("Capture");
		frame.add(canvas);
		frame.addWindowListener(
				new WindowAdapter() { 
					public void windowClosing(WindowEvent e) {
							System.exit(1); 
						} 
				});
		frame.setBounds(new Rectangle(100, 100, 1100, 800));
		frame.setVisible(true);	
		canvas.requestFocus();
	}
	
	public static void main(String[] args) {
		createWindow();
	}
}
